package com.xcesys.extras.remoting.lookup.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xcesys.extras.core.lookup.service.LookupService;
import com.xcesys.extras.core.service.impl.BaseServiceImpl;
import com.xcesys.extras.dataobject.lookup.ValueLabelItem;
import com.xcesys.extras.remoting.lookup.RLookupService;

@Service("RLookupService")
@WebService(endpointInterface = "com.xcesys.extras.remoting.lookup.RLookupService")
public class RLookupServiceImpl extends BaseServiceImpl implements
		RLookupService {

	@Autowired
	private LookupService lookupService = null;

	@Override
	public ValueLabelItem findObjectByCode(String type, String code,
			Object[] conditionValues) {
		return transform(lookupService.findObject(type, code, conditionValues));
	}

	@Override
	public ValueLabelItem findObjectById(String type, Long id,
			Object[] conditionValues) {
		return transform(lookupService.findObject(type, id, conditionValues));
	}

	@Override
	public Collection<ValueLabelItem> findObjectsByCodeLabel(String type,
			String code, String name) {
		return transform(lookupService.findObjects(type, code, name));
	}

	@Override
	public Collection<ValueLabelItem> findObjectsByCondition(String type,
			String code, String name, Object[] conditionValues) {
		return transform(lookupService.findObjects(type, code,
				conditionValues));
	}

	private ValueLabelItem transform(
			com.xcesys.extras.core.model.ValueLabelItem item) {
		if (item == null) {
			return null;
		}
		return new ValueLabelItem(item.getId(), item.getValue(),
				item.getLabel(), item.getDesc());
	}

	private Collection<ValueLabelItem> transform(
			List<com.xcesys.extras.core.model.ValueLabelItem> items) {
		if (items == null) {
			return null;
		}
		Collection<ValueLabelItem> list = new ArrayList<ValueLabelItem>();
		for (com.xcesys.extras.core.model.ValueLabelItem item : items) {
			list.add(transform(item));
		}
		return list;
	}
}
